package com.xigua.xcar.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Rect;
import android.net.Uri;
import android.provider.Contacts;
import android.provider.Contacts.People;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import com.xigua.xcar.R;
import com.xigua.xcar.constant.Constants;
import com.xigua.xcar.constant.LocationPoint;

public class CommonListUtil {

	private static class PopAlertDialog extends AlertDialog {
//		private View view;

		protected PopAlertDialog(Context context, View view) {
			super(context);
//			this.view = view;
		}

		@Override
		public boolean onTouchEvent(MotionEvent event) {
			// view.setBackgroundResource(R.drawable.personal_set_setting01);
			this.cancel();
			return false;
		}
	}

	static private PopAlertDialog mDialog;

	private static Uri insertContact(Context context, String name, String phone) {
		
		ContentValues values = new ContentValues();
		values.put(People.NAME, name);
		
		Uri uri = context.getContentResolver().insert(People.CONTENT_URI,
				values);
		Uri numberUri = Uri.withAppendedPath(uri,
				People.Phones.CONTENT_DIRECTORY);
		values.clear();

		values.put(Contacts.Phones.TYPE, People.Phones.TYPE_MOBILE);
		values.put(People.NUMBER, phone);
		context.getContentResolver().insert(numberUri, values);

		return uri;
	}

	/**
	 * 保存经纬度到sp
	 * */
	public static void saveLocationPointToSP(Context context, int lon, int lat){
		SharedPreferences sp = context.getSharedPreferences(Constants.LOCATIONPOINT, Context.MODE_WORLD_WRITEABLE);
		Editor edit = sp.edit();
		edit.putInt(Constants.LONGATITUE, lon);
		edit.putInt(Constants.LATITUDE, lat);
		edit.commit();
	}
	
	/**
	 * 读取经纬度信息
	 * */
	public static LocationPoint getLocationPointFromSP(Context context){
		LocationPoint lp = new LocationPoint();
		SharedPreferences sp = context.getSharedPreferences(Constants.LOCATIONPOINT, Context.MODE_WORLD_READABLE);
		lp.setLontitude(sp.getInt(Constants.LONGATITUE, 0));
		lp.setLatitude(sp.getInt(Constants.LATITUDE, 0));
		return lp;
	}

	/**
	 * 清除缓存
	 * */
	public static void clearLocationPoint(Context context){
		SharedPreferences sp = context.getSharedPreferences(Constants.LOCATIONPOINT, Context.MODE_WORLD_WRITEABLE);
		Editor edit = sp.edit();
		edit.clear();
		edit.commit();
	}
	
	/* 弹出框 */
	public static void getPopupWindow(final Activity context, final View view,
			final String phoneNum, final String shopName) {
		
		final CharSequence[] items = {"呼叫联系人", "添加到联系人"}; 
		  
		AlertDialog.Builder builder = new AlertDialog.Builder(context); 
		builder.setTitle(shopName); 
		builder.setItems(items, new DialogInterface.OnClickListener() { 
		    public void onClick(DialogInterface dialog, int item) { 
		    	switch(item){
		    	case 0:{
		    		// 拨打联系人
					Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
							+ phoneNum));
					context.startActivity(intent);
		    		break;
		    	}
		    	case 1:{
		    		//添加到联系人
		    		Uri uri = insertContact(context, shopName, phoneNum);
					if (null != uri) {
						Toast.makeText(context, "添加成功", 3000).show();
					} else {
						Toast.makeText(context, "添加失败", 3000).show();
					}
		    		break;
		    	}
		    	}
		    } 
		}); 
		builder.show();
		
		if(null != context){
			return ;
		}
		int[] location = new int[2];
		int x, y;
		int width = view.getWidth();
		int height = view.getHeight();

		view.getLocationOnScreen(location);
		x = location[0];
		y = location[1];

		Rect frame = new Rect();
		context.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
		int statusBarHeight = frame.top;

		mDialog = new PopAlertDialog(context, view);
		mDialog.setOnDismissListener(new OnDismissListener() {
			@Override
			public void onDismiss(DialogInterface dialog) {
			}
		});

		if (mDialog.isShowing()) {
			mDialog.cancel();
			return;
		}
		mDialog.show();
		final Window window = mDialog.getWindow();
		window.setContentView(R.layout.phonenum_menu);
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.dimAmount = 0f;
		lp.gravity = Gravity.TOP;
		lp.y = y + height - statusBarHeight;
		lp.x = x + width - lp.width;
		window.setAttributes(lp);
		Button add = (Button) window.findViewById(R.id.btn_phonemenu_add);
		Button call = (Button) window.findViewById(R.id.btn_phonemenu_call);
//		Button cancel = (Button) window.findViewById(R.id.btn_phonemenu_cancel);
		add.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 添加联系人到通讯录
				Uri uri = insertContact(context, shopName, phoneNum);
				if (null != uri) {
					Toast.makeText(context, "添加成功", 3000).show();
				} else {
					Toast.makeText(context, "添加失败", 3000).show();
				}
				mDialog.cancel();
			}
		});
		call.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 拨打联系人
				Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
						+ phoneNum));
				context.startActivity(intent);
				mDialog.cancel();

			}
		});

//		cancel.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				mDialog.cancel();
//			}
//		});
	}
}
